﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.RouteSetting
{
    public partial class RouteChannelSettingPanel : UserControl, ISettingPanel
    {
        public RouteChannelSettingPanel()
        {
            InitializeComponent();

            this.lvwRoute.SelectedIndexChanged += LvwRoute_SelectedIndexChanged;
        }

        private void LvwRoute_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwRoute.SelectedItems.Count > 0)
            {
                var result_channel = HttpHelper.Get<HResult<List<dl_route_config_vo>>>(AppSettings.Setting.API.GetRouteConfigListById
                    + "?routeId=" + (lvwRoute.SelectedItems[0].Tag as dl_route).id);
                this.lvwChannel.Items.Clear();

                if (result_channel.success)
                {
                    foreach (var ch in result_channel.data)
                    {
                        this.lvwChannel.Items.Add(new ListViewItem(new string[] {
                            (this.lvwChannel.Items.Count+1).ToString(),
                            ch.channel_name,
                            ch.channel_id,
                            ch.storage_server_type,
                            ch.storage_server_ip
                        })).Tag = ch;
                    }
                }
            }
        }

        public void ApplySettings()
        {
            SaveSettings();
        }

        public void LoadSettings()
        {
            var result_route = HttpHelper.Get<HResult<List<dl_route>>>(AppSettings.Setting.API.GetRouteList);
            this.lvwRoute.Items.Clear();
            if (result_route.success)
            {
                foreach (var route in result_route.data)
                {
                    string routeType = "";
                    switch (route.route_type)
                    {
                        case 0:
                            routeType = "对象进点";
                            break;
                        case 1:
                            routeType = "对象换房间";
                            break;
                        case 2:
                            routeType = "对象放风";
                            break;
                        case 3:
                            routeType = "对象离点";
                            break;
                    }
                    this.lvwRoute.Items.Add(new ListViewItem(new string[] {
                        (this.lvwRoute.Items.Count+1).ToString(),
                        route.name,
                        routeType,
                        route.start_location_name,
                        route.start_location_type == 0 ? "留置区域" : (route.start_location_type == 1 ? "放风区域" : "谈话区域"),
                        route.end_location_name,
                        route.end_location_type == 0 ? "留置区域" :  (route.end_location_type == 1 ? "放风区域" : "谈话区域"),
                    })).Tag = route;
                }

                if (lvwRoute.Items.Count > 0)
                {
                    lvwRoute.Items[0].Selected = true;
                }
            }
        }

        public void SaveSettings()
        {
        }

        private void btnAddRoute_Click(object sender, EventArgs e)
        {
            FrmRoute frm = new FrmRoute();
            frm.StartPosition = FormStartPosition.CenterParent;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.Route != null)
                {
                    var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.AddRoute, JsonConvert.SerializeObject(frm.Route));

                    if (result.success)
                    {
                        string routeType = "";
                        switch (frm.Route.route_type)
                        {
                            case 0:
                                routeType = "对象进点、离点";
                                break;
                            case 1:
                                routeType = "对象换房间";
                                break;
                            case 2:
                                routeType = "对象放风";
                                break;
                        }
                        lvwRoute.Items.Add(new ListViewItem(new string[] {
                            (this.lvwRoute.Items.Count+1).ToString(),
                            frm.Route.name,
                            routeType,
                            frm.Route.start_location_name,
                            frm.Route.start_location_type == 0 ? "留置区域" : (frm.Route.start_location_type == 1 ? "放风区域" : "谈话区域"),
                            frm.Route.end_location_name,
                            frm.Route.end_location_type == 0 ? "留置区域" : (frm.Route.end_location_type == 1 ? "放风区域" : "谈话区域"),
                        })).Tag = frm.Route;
                    }
                }
            }
        }

        private void btnEditRoute_Click(object sender, EventArgs e)
        {
            if (this.lvwRoute.SelectedItems.Count > 0)
            {
                FrmRoute frm = new FrmRoute();
                frm.StartPosition = FormStartPosition.CenterParent;
                frm.SetRoute(this.lvwRoute.SelectedItems[0].Tag as dl_route);

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (frm.Route != null)
                    {
                        var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.UpdateRoute, JsonConvert.SerializeObject(frm.Route));

                        if (result.success)
                        {
                            string routeType = "";
                            switch (frm.Route.route_type)
                            {
                                case 0:
                                    routeType = "对象进点";
                                    break;
                                case 1:
                                    routeType = "对象换房间";
                                    break;
                                case 2:
                                    routeType = "对象放风";
                                    break;
                                case 3:
                                    routeType = "对象离点";
                                    break;
                            }
                            lvwRoute.SelectedItems[0].SubItems[1].Text = frm.Route.name;
                            lvwRoute.SelectedItems[0].SubItems[2].Text = routeType;
                            lvwRoute.SelectedItems[0].SubItems[3].Text = frm.Route.start_location_name;
                            lvwRoute.SelectedItems[0].SubItems[4].Text = frm.Route.start_location_type == 0 ? "留置区域" : (frm.Route.start_location_type == 1 ? "放风区域" : "谈话区域");
                            lvwRoute.SelectedItems[0].SubItems[5].Text = frm.Route.end_location_name;
                            lvwRoute.SelectedItems[0].SubItems[6].Text = frm.Route.end_location_type == 0 ? "留置区域" : (frm.Route.end_location_type == 1 ? "放风区域" : "谈话区域");
                            lvwRoute.SelectedItems[0].Tag = frm.Route;
                        }
                    }
                }
            }
        }

        private void btnDelRoute_Click(object sender, EventArgs e)
        {
            if (this.lvwRoute.SelectedItems.Count > 0)
            {
                var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.DeleteRoute,
                    new Dictionary<string, string> {
                        {"routeId", (lvwRoute.SelectedItems[0].Tag as dl_route).id }
                    });

                if (result.success)
                {
                    lvwRoute.SelectedItems[0].Remove();
                }
            }
        }

        private void btnAddChannel_Click(object sender, EventArgs e)
        {
            FrmRouteChannel frm = new FrmRouteChannel();
            frm.StartPosition = FormStartPosition.CenterScreen;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.RouteConfig != null)
                {
                    var route = this.lvwRoute.SelectedItems[0].Tag as dl_route;
                    frm.RouteConfig.route_id = route.id;

                    var ret = HttpHelper.Post<HResult>(AppSettings.Setting.API.AddRouteConfig, JsonConvert.SerializeObject(frm.RouteConfig));

                    if (ret.success)
                    {
                        lvwChannel.Items.Add(new ListViewItem(new string[] {
                            (lvwChannel.Items.Count+1).ToString(),
                            frm.RouteConfig.channel_name,
                            frm.RouteConfig.channel_id,
                            frm.RouteConfig.storage_server_type,
                            frm.RouteConfig.storage_server_ip
                        })).Tag = frm.RouteConfig;
                    }
                }
            }
        }

        private void btnDelChannel_Click(object sender, EventArgs e)
        {
            if (this.lvwChannel.SelectedItems.Count > 0)
            {
                var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.DeleteRouteConfig,
                    new Dictionary<string, string> {
                        {"channelId", (lvwChannel.SelectedItems[0].Tag as dl_route_config_vo).id }
                    });

                if (result.success)
                {
                    lvwChannel.SelectedItems[0].Remove();
                }
            }
        }

        private void btnMap_Click(object sender, EventArgs e)
        {
            if (lvwRoute.SelectedItems.Count > 0)
            {
                FrmRouteMap frm = new FrmRouteMap();
                frm.routeMap.Init("map_2", (lvwRoute.SelectedItems[0].Tag as dl_route).id, (lvwRoute.SelectedItems[0].Tag as dl_route).name);
                frm.StartPosition = FormStartPosition.CenterScreen;

                frm.ShowDialog();
            }


        }
    }
}
